package com.hdax.easybuy.controller;


import com.hdax.easybuy.api.*;
import com.hdax.easybuy.entity.News;
import com.hdax.easybuy.entity.Product;
import com.hdax.easybuy.entity.ProductCategory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/productUI")
public class ProviderUiController {

    @Resource
    private ProductManagerClient productManagerClient;

    @Resource
    private NewsManagerClient newsManagerClient;
    //查看所有的商品//测试
    @RequestMapping("/queryAllProduct12/{id}")
    @ResponseBody
    public List<ProductCategory> queryAllP(@PathVariable("id")int id){
        return productCategoryManagerClient.queryByParentId(id);
    }
    //测试
    @RequestMapping("/queryAllProduct1")
    @ResponseBody
    public List<ProductCategory> queryAllProduct1(){
        List<ProductCategory> listproduct=productCategoryManagerClient.queryByParentId(0);
        for (ProductCategory p1:
                listproduct) {
            List<ProductCategory> listproduct1=productCategoryManagerClient.queryByParentId(p1.getId());
            p1.setProductCategoryList(listproduct1);
            for (ProductCategory p2:
                    listproduct1) {
                List<ProductCategory> listproduct2=productCategoryManagerClient.queryByParentId(p2.getId());
                p2.setProductCategoryList(listproduct2);
            }
        }
        return listproduct;
    }
    //测试新闻资讯是否正确
    @RequestMapping("/queryAllProducts")
    @ResponseBody
    public List<News> quersyAllProduct1(){

        return newsManagerClient.getAllNews();
    }
    @Resource
    private ProductCategoryManagerClient productCategoryManagerClient;


    //往页面上转的controller
    @RequestMapping("/queryAllProduct")
    public String queryAllP(Model model){
        Map<Integer,List<Product>> map=new HashMap<>();
        //得到所有的商品类别信息
        List<ProductCategory> queryAllCategroy=productCategoryManagerClient.queryCategoryAll();
        //得到父分类信息
        List<ProductCategory> ProductCategoryList=new ArrayList<>();
        //得到所有的上商品信息
        List<Product> productList=productManagerClient.queryAll();
        // model.addAttribute("All",map);
        model.addAttribute("AllGoods",productList);
       // model.addAttribute("AllFenLei",ProductCategoryList);
        //model.addAttribute("erji",list1);

        List<ProductCategory> listproduct=productCategoryManagerClient.queryByParentId(0);
        for (ProductCategory p1:
             listproduct) {
            List<ProductCategory> listproduct1=productCategoryManagerClient.queryByParentId(p1.getId());
            p1.setProductCategoryList(listproduct1);
            for (ProductCategory p2:
                    listproduct1) {
                List<ProductCategory> listproduct2=productCategoryManagerClient.queryByParentId(p2.getId());
                p2.setProductCategoryList(listproduct2);
            }
        }
        model.addAttribute("fenleibiao",listproduct);


        //跳页面时，带新闻信息
        List<News> allNews = newsManagerClient.getAllNews();
        model.addAttribute("getNewsSouYou",allNews);

        return "pre/index";
    }

    //商品详情
    @RequestMapping("/queryid1")
    //@ResponseBody
    public String queryid1(int id,Model model){
        //model.addAttribute("product",productManagerClient.getAll());
        model.addAttribute("good",productManagerClient.queryById(id));
        return "/pre/product/productDetail";
    }
}
